﻿Shader "Custom/FX/LX_FX_DistortCommandBuffer"
{
	Properties
	{
		_NoiseTex("NoiseTexture", 2D) = "white" {}
		_DistortStrength("DistortStrength", Range(0,2)) = 0.2
		_DistortTimeFactor("DistortTimeFactor", Range(0,2)) = 1
		_Offset("Offset", float) = 0
	}

		SubShader
		{
			Tags
				{
					"RenderType" = "Transparent"
					"Queue" = "Transparent"
			"IgnoreProjector" = "True"
			"PreviewType" = "Plane"
					"DisableBatching" = "True"
				}
			LOD 100
			Cull Off Lighting Off ZWrite Off

			Pass
			{
					Blend SrcAlpha OneMinusSrcAlpha
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
				fixed4 color : COLOR;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				float4 grabPos : TEXCOORD1;
				float4 pos : SV_POSITION;
				fixed4 color : TEXCOORD2;
			};

			sampler2D _NoiseTex; float4 _NoiseTex_ST;
			sampler2D _ScreenColorTexture;
			sampler2D _ScreenDepthTexture;
			fixed _DistortStrength;
			fixed _DistortTimeFactor;
			fixed _Offset;

			v2f vert(appdata v)
			{
				v2f o;
				o.pos = UnityObjectToClipPos(v.vertex);
				o.uv.xy = TRANSFORM_TEX(v.uv, _NoiseTex);
				o.color = v.color;
				o.grabPos = ComputeScreenPos(o.pos);
				COMPUTE_EYEDEPTH(o.grabPos.z);
				return o;
			}

			fixed4 frag(v2f i) : SV_Target
			{
				float Zbuffer = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_ScreenDepthTexture, UNITY_PROJ_COORD(i.grabPos)));
				float2 offset = tex2D(_NoiseTex, i.uv.xy - _Time.y * _DistortTimeFactor).xy;
				i.grabPos.xy -= offset.xy  * _DistortStrength * i.color.a;
				fixed4 col = tex2Dproj(_ScreenColorTexture, i.grabPos + _Offset) * saturate(Zbuffer - i.grabPos.z);
				return col;
			}
			ENDCG
			}
		}
}